package com.google.location.im.ui;

import java.util.Hashtable;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;

import de.enough.polish.ui.Alert;
import de.enough.polish.util.Locale;

/**
 * @author tharindu
 */
public class MainUI
{

    // Screen codes: unused regard to memory
    final public static int SCR_SPLASH = 0;

    final public static int SCR_LOGIN = 1;

    final public static int SCR_SEARCH = 2;

    final public static int SCR_MAP = 3;

    // Commands codes:
    final public static int CMD_OK = 1;

    final public static int CMD_CANCEL = 2;

    final public static int CMD_YES = 3;

    final public static int CMD_NO = 4;

    final public static int CMD_BACK = 5;

    final public static int CMD_EXIT = 6;

    // Commands:
    final private static Command cmdOk = new Command(Locale.get("locateim.command.ok"), Command.OK, 5);// [LOCALE]

    final private static Command cmdCancel = new Command(Locale.get("locateim.command.cancel"), Command.CANCEL, 1);// [LOCALE]

    final private static Command cmdYes = new Command(Locale.get("locateim.command.yes"), Command.OK, 1);// [LOCALE]

    final private static Command cmdNo = new Command(Locale.get("locateim.command.no"), Command.CANCEL, 2);// [LOCALE]

    final private static Command cmdBack = new Command(Locale.get("locateim.command.back"), Command.BACK, 1);// [LOCALE]

    final private static Command cmdExit = new Command(Locale.get("locateim.command.exit"), Command.CANCEL, 1);// [LOCALE]

    // commands list
    static private Hashtable commands_ = new Hashtable();

    // Associate commands and commands codes
    static {
        commands_.put(new Integer(CMD_OK), cmdOk);
        commands_.put(new Integer(CMD_CANCEL), cmdCancel);
        commands_.put(new Integer(CMD_YES), cmdYes);
        commands_.put(new Integer(CMD_NO), cmdNo);
        commands_.put(new Integer(CMD_BACK), cmdBack);
        commands_.put(new Integer(CMD_EXIT), cmdExit);
    }

    // screens:
    static private Splash scrSplash;

    static private LoginScreen scrLogin;

    static private SearchScreen scrSearch;

    static private MapCanvas scrMapCanvas;

    static private MainUI ui_; // HsenidUI object

    static private MainMidlet locateIm_; // MainMidlet object
    
    static private Thread mapThread_; // Thread to draw the map

    public MainUI()
    {
        ui_ = this;
        locateIm_ = MainMidlet.getInstance();
        scrSplash = new Splash();
        scrLogin = new LoginScreen();
        scrSearch = new SearchScreen();
        scrMapCanvas = new MapCanvas("GoogleMap", null);
    }

    public static MainUI getInstance()
    {
        return ui_;
    }

    public Hashtable getCommands()
    {
        return commands_;
    }

    public static void hsenidCommand(Command c, Displayable d)
    {
        // commands for login screen
        if (d == scrLogin) {
            if (c == cmdOk) {
                if (scrLogin.isValidInput()) {
                    ui_.setView(SCR_SEARCH);
                } else {
                    ui_.setAlert(Locale.get("locateim.login.ui.alert"), d);
                }
            } else if (c == cmdExit) {
                locateIm_.notifyDestroyed();
            }
        }
        // commands for search screen
        else if (d == scrSearch) {
            if (c == cmdOk) {
                if (scrSearch.isValidInput()) {
                    ui_.setView(SCR_MAP);
                    scrMapCanvas.init("Colombo");
                    scrMapCanvas.start();
                } else {
                    ui_.setAlert(Locale.get("locateim.login.ui.alert"), d);
                }
            } else if (c == cmdExit) {
                locateIm_.notifyDestroyed();
            }
        }
        // commands for map canvas screen
        else if (d == scrMapCanvas) {
            ui_.setView(SCR_SEARCH);
        }

    }

    public void setView(int display)
    {
        // set display according to screen code
        switch (display) {
            case SCR_SPLASH:
                locateIm_.setDisplay(scrSplash);
                break;
            case SCR_LOGIN:
                locateIm_.setDisplay(scrLogin);
                break;
            case SCR_MAP:
                locateIm_.setDisplay(scrMapCanvas);
                break;
            case SCR_SEARCH:
                locateIm_.setDisplay(scrSearch);
        }
    }

    public void setAlert(String text, Displayable displable)
    {
        // #style confirmationAlert
        Alert alert = new Alert(null);

        // #style alertGradient
        alert.setString(text);

        Display testDisplay = Display.getDisplay(MainMidlet.getInstance());
        Alert.setCurrent(testDisplay, alert, displable);
    }
}
